import 'dart:async';

import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../models/journalItem.dart';
import '../utils/screw.dart';

class JournalItemDialog extends StatefulWidget {
  JournalItemDialog({Key key, this.item}) : super(key: key);

  final JournalItem item;

  @override
  _JournalItemDialogState createState() => _JournalItemDialogState();
}

class _JournalItemDialogState extends State<JournalItemDialog> {
  final _detail = TextEditingController();
  final _categories = <String>['事务', '计划', '备忘'];
  String _month, _date;
  String _category;
  final GlobalKey _formKey = new GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    _category = _categories.first;
    if (widget.item.id == null || widget.item.id == 0) {
      _month = Screw.genMonthString();
      _date = Screw.genDateString();
    } else {
      _month = widget.item.month;
      _date = widget.item.date;
      _category = widget.item.category;
      _detail.text = widget.item.detail;
    }
  }

  void _onSubmit() {
    var item = JournalItem(
      id: 0,
      month: _month,
      date: _date,
      category: _category,
      detail: _detail.text,
    );
    Navigator.pop(context, item);
  }

  Widget _genDropDownButton() {
    return DropdownButton<String>(
      isExpanded: true,
      hint: Text(_categories.first, style: TextStyle(fontSize: 18)),
      value: _category,
      style: TextStyle(fontSize: 18, color: Colors.black),
      items: _categories
          .map((e) => DropdownMenuItem<String>(
                value: e,
                child: Text(e),
              ))
          .toList(),
      onChanged: (v) {
        setState(() {
          _category = v;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.8,
      height: MediaQuery.of(context).size.height * 0.5,
      child: FlatPanel(
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              _genDropDownButton(),
              TextFormField(
                maxLines: null,
                controller: _detail,
                decoration: InputDecoration(
                  hintText: '内容',
                  prefixIcon: Icon(Icons.list),
                ),
                validator: (v) => v.trim().length > 0 ? null : '内容不能为空',
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: ElevatedButton(
                        child: Text('清空', style: getTextStyle(fontSize: 18)),
                        onPressed: () {
                          (_formKey.currentState as FormState).reset();
                        }),
                  ),
                  Expanded(
                    flex: 1,
                    child: ElevatedButton(
                      child: Text(widget.item.id == 0 ? '提交' : '保存',
                          style: getTextStyle(fontSize: 18)),
                      onPressed: () {
                        if ((_formKey.currentState as FormState).validate())
                          _onSubmit();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

Future<JournalItem> showJournalItemDialog(BuildContext context,
    {JournalItem item}) async {
  return await showDialog<JournalItem>(
      context: context,
      builder: (context) {
        return Dialog(child: JournalItemDialog(item: item));
      });
}
